package com.erp.gather.controller;
import com.erp.gather.domain.bo.ErpSellerStoreProductGroupBo;
import com.erp.gather.domain.vo.ErpSellerStoreProductGroupVo;
import com.erp.gather.service.IErpSellerStoreProductGroupService;
import com.ruoyi.ae.api.RemoteAeStoreGroupService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.gather.api.domain.dto.SendProductGroupDto;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户店铺商品分组关联
 *
 * @author erp
 * @date 2023-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/sellerStoreProductGroup")
public class ErpSellerStoreProductGroupController extends BaseController {

    private final IErpSellerStoreProductGroupService iErpSellerStoreProductGroupService;

    @Resource
    RemoteAeStoreGroupService remoteAeStoreGroupService;

    /**
     * 查询用户店铺商品分组关联列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStoreProductGroupVo> list(ErpSellerStoreProductGroupBo bo, PageQuery pageQuery) {
        return iErpSellerStoreProductGroupService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取用户店铺商品分组关联详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpSellerStoreProductGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                                   @PathVariable Long id) {
        return R.ok(iErpSellerStoreProductGroupService.queryById(id));
    }

    /**
     * 新增用户店铺商品分组关联
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreProductGroupBo bo) {
        return toAjax(iErpSellerStoreProductGroupService.insertByBo(bo));
    }

    /**
     * 修改用户店铺商品分组关联
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreProductGroupBo bo) {
        return toAjax(iErpSellerStoreProductGroupService.updateByBo(bo));
    }

    /**
     * 删除用户店铺商品分组关联
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerStoreProductGroupService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/bindingProdudtForGroup")
    public R<Object> bindingProdudtForGroup(String productId, Long groupId) {
        R<Object> objectR = iErpSellerStoreProductGroupService.bindingProdudtForGroup(productId, groupId);
        if (objectR.getCode() == 200) {
            Map<String, Object> map = (Map<String, Object>) objectR.getData();
//            SendProductGroupDto dto = new SendProductGroupDto();
//            dto.setStoreId((String) map.get("storeId"));
//            dto.setProductGroups((String) map.get("productGroups"));
//            dto.setGroupId((String) map.get("groupId"));
            iErpSellerStoreProductGroupService.sendProductGroupToAe(map);
            return R.ok("添加商品到分组成功,请稍后");
        } else {
            return objectR;
        }

    }

    //同步分组内商品至速卖通
    @GetMapping("/synchronizeProductGroup")
    public R<Object> synchronizeProductGroup(String groupId) {
        //TODO Ae相关 转为内部调用 待测试
        return iErpSellerStoreProductGroupService.synchronizeProductGroup(groupId);
    }





}
